/**
 * op:{
        preInfo: val1,
        preInfoLabel: val2
      }
 */
export default {
  eq(op) {
    if (!op) {
      return op;
    }
    if (!op.preInfo_style) {
      op.preInfo_style = "background:#00c0ef;color:#fff";
    }
    if (!op.preInfoLabel_style) {
      op.preInfoLabel_style = "background:red;color:#fff";
    }
    var ps = {
      v1_i: 0,
      v1_new_value: "",
      v2_i: 0,
      v2_new_value: ""
    };
    while (
      ps.v1_i < op.preInfo.length &&
      ps.v2_i < op.preInfoLabel.length
    ) {
      if (op.preInfo[ps.v1_i] == op.preInfoLabel[ps.v2_i]) {
        ps.v1_new_value += op.preInfo[ps.v1_i]
          .replace(/</g, "<")
          .replace(">", ">");
        ps.v2_new_value += op.preInfoLabel[ps.v2_i]
          .replace(/</g, "<")
          .replace(">", ">");
        ps.v1_i += 1;
        ps.v2_i += 1;
        if (ps.v1_i >= op.preInfo.length) {
          ps.v2_new_value +=
            "<span style='" +
            op.preInfoLabel_style +
            "'>" +
            op.preInfoLabel
              .substr(ps.v2_i)
              .replace(/</g, "<")
              .replace(">", ">") +
            "</span>";
          break;
        }
        if (ps.v2_i >= op.preInfoLabel.length) {
          ps.v1_new_value +=
            "<span style='" +
            op.preInfo_style +
            "'>" +
            op.preInfo
              .substr(ps.v1_i)
              .replace(/</g, "<")
              .replace(">", ">") +
            "</span>";
          break;
        }
      } else {
        ps.v1_index = ps.v1_i + 1;
        ps.v1_eq_length = 0;
        ps.v1_eq_max = 0;
        ps.v1_start = ps.v1_i + 1;
        while (ps.v1_index < op.preInfo.length) {
          if (
            op.preInfo[ps.v1_index] ==
            op.preInfoLabel[ps.v2_i + ps.v1_eq_length]
          ) {
            ps.v1_eq_length += 1;
          } else if (ps.v1_eq_length > 0) {
            if (ps.v1_eq_max < ps.v1_eq_length) {
              ps.v1_eq_max = ps.v1_eq_length;
              ps.v1_start = ps.v1_index - ps.v1_eq_length;
            }
            ps.v1_eq_length = 0;
            break; //只寻找最近的
          }
          ps.v1_index += 1;
        }
        if (ps.v1_eq_max < ps.v1_eq_length) {
          ps.v1_eq_max = ps.v1_eq_length;
          ps.v1_start = ps.v1_index - ps.v1_eq_length;
        }

        ps.v2_index = ps.v2_i + 1;
        ps.v2_eq_length = 0;
        ps.v2_eq_max = 0;
        ps.v2_start = ps.v2_i + 1;
        while (ps.v2_index < op.preInfoLabel.length) {
          if (
            op.preInfoLabel[ps.v2_index] ==
            op.preInfo[ps.v1_i + ps.v2_eq_length]
          ) {
            ps.v2_eq_length += 1;
          } else if (ps.v2_eq_length > 0) {
            if (ps.v2_eq_max < ps.v2_eq_length) {
              ps.v2_eq_max = ps.v2_eq_length;
              ps.v2_start = ps.v2_index - ps.v2_eq_length;
            }
            ps.v1_eq_length = 0;
            break; //只寻找最近的
          }
          ps.v2_index += 1;
        }
        if (ps.v2_eq_max < ps.v2_eq_length) {
          ps.v2_eq_max = ps.v2_eq_length;
          ps.v2_start = ps.v2_index - ps.v2_eq_length;
        }
        if (ps.v1_eq_max < op.eq_min && ps.v1_start - ps.v1_i > op.eq_index) {
          ps.v1_eq_max = 0;
        }
        if (ps.v2_eq_max < op.eq_min && ps.v2_start - ps.v2_i > op.eq_index) {
          ps.v2_eq_max = 0;
        }
        if (ps.v1_eq_max == 0 && ps.v2_eq_max == 0) {
          ps.v1_new_value +=
            "<span style='" +
            op.preInfo_style +
            "'>" +
            op.preInfo[ps.v1_i].replace(/</g, "<").replace(">", ">") +
            "</span>";
          ps.v2_new_value +=
            "<span style='" +
            op.preInfoLabel_style +
            "'>" +
            op.preInfoLabel[ps.v2_i]
              .replace(/</g, "<")
              .replace(">", ">") +
            "</span>";
          ps.v1_i += 1;
          ps.v2_i += 1;

          if (ps.v1_i >= op.preInfo.length) {
            ps.v2_new_value +=
              "<span style='" +
              op.preInfoLabel_style +
              "'>" +
              op.preInfoLabel
                .substr(ps.v2_i)
                .replace(/</g, "<")
                .replace(">", ">") +
              "</span>";
            break;
          }
          if (ps.v2_i >= op.preInfoLabel.length) {
            ps.v1_new_value +=
              "<span style='" +
              op.preInfo_style +
              "'>" +
              op.preInfo
                .substr(ps.v1_i)
                .replace(/</g, "<")
                .replace(">", ">") +
              "</span>";
            break;
          }
        } else if (ps.v1_eq_max > ps.v2_eq_max) {
          ps.v1_new_value +=
            "<span style='" +
            op.preInfo_style +
            "'>" +
            op.preInfo
              .substr(ps.v1_i, ps.v1_start - ps.v1_i)
              .replace(/</g, "<")
              .replace(">", ">") +
            "</span>";
          ps.v1_i = ps.v1_start;
        } else {
          ps.v2_new_value +=
            "<span style='" +
            op.preInfoLabel_style +
            "'>" +
            op.preInfoLabel
              .substr(ps.v2_i, ps.v2_start - ps.v2_i)
              .replace(/</g, "<")
              .replace(">", ">") +
            "</span>";
          ps.v2_i = ps.v2_start;
        }
      }
    }
    op.preInfo = ps.v1_new_value;
    op.preInfoLabel = ps.v2_new_value;
    return op;
  }
}
